home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / widgets.zip / WTEST.C < prev    next >
C/C++ Source or Header  |  1992-07-22  |  8KB  |  307 lines

  1. //  WTEST
  2. //
  3. //  David Stafford
  4. //
  5. //  11/25/91
  6. //  01/05/92
  7. //
  8. //  Test program for the Widget library.
  9.  
  10.  
  11. #include <windows.h>
  12. #include <stdlib.h>
  13. #include "widget.h"
  14.  
  15.  
  16. HWND    MainWindow;                      // The main window
  17. HANDLE  hInst;                           // The instance
  18.  
  19. HBITMAP PatternBits;                     // Background bitmap pattern
  20. HCURSOR PatternBrush;                    // Background bitmap brush
  21.  
  22. HCURSOR Hand;
  23.  
  24. RECT    ButterflyBounds;
  25. BOOL    MouseTracking;                   // Are we pulling the card around?
  26. POINT   MouseLoc;
  27.  
  28. HBITMAP ButterflyBits, ButterflyMask;    // Butterfly data
  29. HWIDGET ButterflyWidget;
  30. POINT   ButterflyDir;
  31. POINT   ButterflyLoc;
  32.  
  33. HBITMAP CardBits, CardMask;              // Card data
  34. HWIDGET CardWidget;
  35. POINT   CardOffset;
  36.  
  37.  
  38. void Init( void )
  39.   {
  40.   ButterflyBits = LoadBitmap( hInst, MAKEINTRESOURCE( 100 ) );
  41.   ButterflyMask = LoadBitmap( hInst, MAKEINTRESOURCE( 101 ) );
  42.   CardBits      = LoadBitmap( hInst, MAKEINTRESOURCE( 110 ) );
  43.   CardMask      = LoadBitmap( hInst, MAKEINTRESOURCE( 111 ) );
  44.   PatternBits   = LoadBitmap( hInst, MAKEINTRESOURCE( 200 ) );
  45.   Hand          = LoadCursor( hInst, MAKEINTRESOURCE( 300 ) );
  46.  
  47.   PatternBrush  = CreatePatternBrush( PatternBits );
  48.  
  49.   ButterflyWidget = CreateWidget( MainWindow, ButterflyBits, ButterflyMask, 0 );
  50.   CardWidget      = CreateWidget( MainWindow, CardBits, CardMask, 0 );
  51.  
  52.   ShowWidget( NULL, CardWidget );
  53.   ShowWidget( NULL, ButterflyWidget );
  54.   }
  55.  
  56.  
  57. void Cleanup( void )
  58.   {
  59.   DestroyAllWidgetsForTheWindow( MainWindow );
  60.  
  61.   DeleteObject( ButterflyBits );
  62.   DeleteObject( ButterflyMask );
  63.   DeleteObject( CardBits );
  64.   DeleteObject( CardMask );
  65.   DeleteObject( PatternBits );
  66.   DeleteObject( PatternBrush );
  67.  
  68.   DestroyCursor( Hand );
  69.   }
  70.  
  71.  
  72. long SquRoot( long Val )
  73.   {
  74.   long Lo = 0, Hi = 32768, Mid, Mid2;
  75.  
  76.   while( Lo <= Hi )
  77.     {
  78.     Mid = (Lo + Hi) / 2;
  79.  
  80.     Mid2 = Mid * Mid;
  81.  
  82.     if( Mid2 == Val )  return( Mid );
  83.  
  84.     if( Mid2 < Val )  Lo = Mid + 1;
  85.     else              Hi = Mid - 1;
  86.     }
  87.  
  88.   return( Mid );
  89.   }
  90.  
  91.  
  92. void Animate( void )
  93.   {
  94.   POINT NewButterflyLoc, Bounce;
  95.   LONG xDis, yDis, Total;
  96.   int xPull, yPull;
  97.   static int Count = 400;
  98.  
  99.   if( IsWidgetVisible( ButterflyWidget ) )
  100.     {
  101.     if( --Count == 0 )
  102.       {
  103.       Count = 400;
  104.  
  105.       ButterflyDir.x = (ButterflyDir.x * 20) / 19;
  106.       ButterflyDir.y = (ButterflyDir.y * 20) / 19;
  107.       }
  108.  
  109.     xDis = abs( MouseLoc.x - (ButterflyLoc.x / 8) );
  110.     yDis = abs( MouseLoc.y - (ButterflyLoc.y / 8) );
  111.  
  112.     Total = SquRoot( xDis * xDis + yDis * yDis );
  113.  
  114.     if( Total != 0 )
  115.       {
  116.       xPull = ((Total * xDis) / (xDis + yDis)) / 32;
  117.       yPull = ((Total * yDis) / (xDis + yDis)) / 32;
  118.  
  119.       if( xPull > 30 )  xPull = 30;
  120.       if( yPull > 30 )  yPull = 30;
  121.  
  122.       if( MouseLoc.x < ButterflyLoc.x / 8 )  xPull *= -1;
  123.       if( MouseLoc.y < ButterflyLoc.y / 8 )  yPull *= -1;
  124.  
  125.       ButterflyDir.x += xPull;
  126.       ButterflyDir.y += yPull;
  127.       }
  128.  
  129.     NewButterflyLoc.x = ButterflyLoc.x + ButterflyDir.x;
  130.     NewButterflyLoc.y = ButterflyLoc.y + ButterflyDir.y;
  131.  
  132.     Bounce.x = Bounce.y = 0;
  133.  
  134.     if( NewButterflyLoc.x >= ButterflyBounds.right || NewButterflyLoc.x < ButterflyBounds.left )  Bounce.x = 1;
  135.     if( NewButterflyLoc.y >= ButterflyBounds.bottom || NewButterflyLoc.y < ButterflyBounds.top )  Bounce.y = 1;
  136.  
  137.     if( Bounce.x )  ButterflyDir.x = (ButterflyDir.x * -4) / 5;
  138.     if( Bounce.y )  ButterflyDir.y = (ButterflyDir.y * -4) / 5;
  139.  
  140.     ButterflyLoc.x += ButterflyDir.x;
  141.     ButterflyLoc.y += ButterflyDir.y;
  142.  
  143.     MoveWidget( NULL, ButterflyWidget, ButterflyLoc.x / 8, ButterflyLoc.y / 8 );
  144.     }
  145.   }
  146.  
  147.  
  148. long FAR PASCAL _export WndProc( HWND hWnd, unsigned iMessage, WORD wParam, LONG lParam )
  149.   {
  150.   HDC DC;
  151.   PAINTSTRUCT Paint;
  152.   POINT Pt;
  153.   RECT Rect;
  154.  
  155.   switch( iMessage )
  156.     {
  157.     case WM_DESTROY:
  158.       PostQuitMessage( 0 );
  159.       break;
  160.  
  161.     case WM_MOUSEMOVE:
  162.       MouseLoc.x = LOWORD( lParam );
  163.       MouseLoc.y = HIWORD( lParam );
  164.  
  165.       if( MouseTracking || IsPointInWidget( CardWidget, MouseLoc.x, MouseLoc.y ) )
  166.         {
  167.         SetCursor( Hand );
  168.         }
  169.       else
  170.         {
  171.         SetCursor( LoadCursor( NULL, MAKEINTRESOURCE( IDC_ARROW ) ) );
  172.         }
  173.  
  174.       if( MouseTracking )
  175.         {
  176.         ShowCursor( FALSE );
  177.         MoveWidget( NULL, CardWidget, MouseLoc.x - CardOffset.x, MouseLoc.y - CardOffset.y );
  178.         ShowCursor( TRUE );
  179.         }
  180.       break;
  181.  
  182.     case WM_LBUTTONDOWN:
  183.       HideWidget( NULL, ButterflyWidget );
  184.       SetCapture( hWnd );
  185.       Pt = GetWidgetPoint( CardWidget );
  186.  
  187.       if( IsPointInWidget( CardWidget, LOWORD( lParam ), HIWORD( lParam ) ) )
  188.         {
  189.         MouseTracking = TRUE;
  190.  
  191.         CardOffset.x = LOWORD( lParam ) - Pt.x;
  192.         CardOffset.y = HIWORD( lParam ) - Pt.y;
  193.         }
  194.       else
  195.         {
  196.         HideWidget( NULL, ButterflyWidget );
  197.  
  198.         AnimateWidget( NULL,
  199.                        CardWidget,
  200.                        LOWORD( lParam ) - GetWidgetSize( CardWidget ).x / 2,
  201.                        HIWORD( lParam ) - GetWidgetSize( CardWidget ).y / 2,
  202.                        DistanceInPoints( Pt.x, Pt.y, LOWORD( lParam ), HIWORD( lParam ) ),
  203.                        NULL );
  204.  
  205.         ShowWidget( NULL, ButterflyWidget );
  206.         }
  207.       break;
  208.  
  209.     case WM_LBUTTONUP:
  210.       MouseTracking = FALSE;
  211.  
  212.       if( !IsWidgetVisible( ButterflyWidget ) )  ShowWidget( NULL, ButterflyWidget );
  213.       ReleaseCapture();
  214.       break;
  215.  
  216.     case WM_SIZE:
  217.       ButterflyDir.x = ButterflyDir.y = 1;
  218.       ButterflyLoc.x = ButterflyLoc.y = 0;
  219.  
  220.       MoveWidget( NULL, ButterflyWidget, 0, 0 );
  221.       MoveWidget( NULL, CardWidget, 0, 0 );
  222.  
  223.       ButterflyBounds.left   = GetWidgetSize( ButterflyWidget ).x * -8;
  224.       ButterflyBounds.top    = GetWidgetSize( ButterflyWidget ).y * -8;
  225.       ButterflyBounds.right  = LOWORD( lParam ) * 8;
  226.       ButterflyBounds.bottom = HIWORD( lParam ) * 8;
  227.       break;
  228.  
  229.     case WM_PAINT:
  230.       GetClientRect( hWnd, &Rect );
  231.  
  232.       InvalidateWidgetsForPaint( hWnd );
  233.  
  234.       DC = BeginPaint( hWnd, &Paint );
  235.  
  236.       FillRect( DC, &Rect, PatternBrush );
  237.  
  238.       RepaintWidgets( DC, hWnd );
  239.  
  240.       EndPaint( hWnd, &Paint );
  241.       break;
  242.  
  243.     default:
  244.       return( DefWindowProc( hWnd, iMessage, wParam, lParam ) );
  245.     }
  246.  
  247.   return( 0L );
  248.   }
  249.  
  250.  
  251. int PASCAL WinMain( HANDLE hInstance, HANDLE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow )
  252.   {
  253.   MSG       msg;
  254.   WNDCLASS  wndclass;
  255.  
  256.   hInst = hInstance;
  257.  
  258.   if( !hPrevInstance )
  259.     {
  260.     wndclass.style         = CS_HREDRAW | CS_VREDRAW;
  261.     wndclass.lpfnWndProc   = WndProc;
  262.     wndclass.cbClsExtra    = 0;
  263.     wndclass.cbWndExtra    = 0;
  264.     wndclass.hInstance     = hInstance;
  265.     wndclass.hIcon         = LoadIcon( NULL, IDI_EXCLAMATION );
  266.     wndclass.hCursor       = NULL;
  267.     wndclass.hbrBackground = NULL;
  268.     wndclass.lpszMenuName  = NULL;
  269.     wndclass.lpszClassName = "XFIRE";
  270.  
  271.     if( !RegisterClass( &wndclass ) )  return( FALSE );
  272.     }
  273.  
  274.   MainWindow = CreateWindow( "XFIRE",
  275.                              "Widget Test Program",
  276.                              WS_OVERLAPPEDWINDOW,
  277.                              CW_USEDEFAULT,
  278.                              0,
  279.                              CW_USEDEFAULT,
  280.                              0,
  281.                              NULL,
  282.                              NULL,
  283.                              hInstance,
  284.                              NULL );
  285.   Init();
  286.  
  287.   ShowWindow( MainWindow, nCmdShow );
  288.   UpdateWindow( MainWindow );
  289.  
  290.   do
  291.     {
  292.     while( !PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
  293.       {
  294.       Animate();
  295.       }
  296.  
  297.     DispatchMessage( &msg );
  298.     TranslateMessage( &msg );
  299.     }
  300.   while( msg.message != WM_QUIT );
  301.  
  302.   Cleanup();
  303.  
  304.   return( msg.wParam );
  305.   }
  306.  
  307.